package com.xgry.bpm.api.business.gateway.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xgry.bpm.api.business.gateway.entity.TBGateway;
import com.xgry.bpm.api.business.gateway.form.TBGatewayForm;
import com.xgry.bpm.api.business.gateway.service.TBGatewayService;
import com.xgry.bpm.api.business.gateway.vo.TBGatewayVO;
import com.xgry.bpm.common.utils.EasyPoiUtils;
import com.xgry.bpm.common.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

;

/**
 * <p>
 * TBGateway前端控制器
 *
 * </p>
 *
 * @author admin
 * @since 2020-07-15
 */
@RestController
@Api(tags = "TBGateway")
@RequestMapping("/tBGateway")
public class TBGatewayController {
    @Autowired
    public TBGatewayService tBGatewayService;

    /**
     * 保存单条
     *
     * @param param 保存参数
     * @return 是否添加成功
     */
    @ApiOperation(value = "保存", notes = "保存数据到TBGateway")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updateBy", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_name", value = "门户名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_url", value = "门户url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_icon", value = "门户图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_sort", value = "门户排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_code", value = "门户编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
    })
    @RequestMapping(value = "/add.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result addTBGateway(@RequestBody TBGatewayForm param) {
        if (1 != tBGatewayService.save(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }

    /**
     * 更新(根据主键id更新)
     *
     * @param param 修改参数
     * @return 是否更改成功
     */
    @ApiOperation(value = "更新数据", notes = "根据主键id更新TBGateway数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updateBy", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_name", value = "门户名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_url", value = "门户url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_icon", value = "门户图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_sort", value = "门户排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_code", value = "门户编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
    })
    @RequestMapping(value = "/updateById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result updateTBGatewayById(@RequestBody TBGatewayForm param) {
        if (1 != tBGatewayService.updateById(param)) {
            return Result.fail("保存失败");
        }
        return Result.success();
    }


    /**
     * 删除(根据主键id删除)
     *
     * @param //主键id
     * @return 是否删除成功
     */
    @ApiOperation(value = "删除数据", notes = "根据主键id删除TBGateway数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @RequestMapping(value = "/deleteById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @Transactional
    public Result deleteTBGatewayById(String[] ids) {
        int result = 0;
        for (String id : ids) {
            tBGatewayService.deleteById(id);
            result++;
        }

        if (result > 0) {
            return Result.fail("删除失败！");
        }

        return Result.success("操作成功");
    }

    /**
     * 根据主键id查询单条
     *
     * @param // 主键id
     * @return 查询结果
     */
    @ApiOperation(value = "获取单条数据", notes = "根据主键id获取TBGateway数据")
    @ApiImplicitParam(paramType = "query", name = "", value = "Id", required = true, dataType = "String")
    @RequestMapping(value = "/getById.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBGatewayById(@RequestParam(value = "Id") String id) {
        TBGatewayVO result = tBGatewayService.selectById(id);
        if (null == result) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }

    /**
     * 查询全部
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "全部查询", notes = "查询TBGateway全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updateBy", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_name", value = "门户名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_url", value = "门户url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_icon", value = "门户图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_sort", value = "门户排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_code", value = "门户编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryAll.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBGatewayAll(@RequestBody TBGatewayForm param) {
        List<TBGatewayVO> result = tBGatewayService.selectAll(param);
        if (CollectionUtils.isEmpty(result)) {
            return Result.fail("无数据！");
        }
        return Result.success("操作成功", result);
    }

    /**
     * 分页查询
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询", notes = "分页查询TBGateway全部数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "按钮主键", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "version", value = "乐观锁", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_by", value = "创建人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "created_time", value = "创建时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "updateBy", value = "更新人", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "update_time", value = "更新时间", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_name", value = "门户名称", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_url", value = "门户url", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_icon", value = "门户图标", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_sort", value = "门户排序号", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_code", value = "门户编码", required = false, dataType = ""),
            @ApiImplicitParam(paramType = "query", name = "v_gateway_state", value = "模块状态 0:启用 1：停用", required = false, dataType = ""),
    })
    @RequestMapping(value = "/queryPage.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result getTBGatewayPage(@RequestBody TBGatewayForm param) {
        IPage<TBGatewayVO> result = tBGatewayService.selectPage(param);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return Result.fail("无数据！");
        }
        return Result.convertIpageToSuccess(result);
    }

    /**
     * Excel导入
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导入", notes = "导入全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "", value = "", required = false, dataType = "String")
    })
    @RequestMapping(value = "/importExcel.do", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Result importExcel(@RequestParam MultipartFile file) throws Exception {
        List<TBGateway> list = EasyPoiUtils.importExcel(file, 0, 1, TBGateway.class);
        if (CollectionUtils.isEmpty(list)) {
            return Result.fail("无数据");
        }
        return Result.success();
    }

    /**
     * Excel导出
     *
     * @param //param 查询条件
     * @return 查询结果
     */
    @ApiOperation(value = "Excel导出", notes = "导出全字段")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "size", value = "每页条数", required = false, dataType = "String")

    })
    @RequestMapping(value = "/exportExcel.do", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public void exportExcel(HttpServletResponse response) throws Exception {
        List<TBGateway> result = tBGatewayService.list();
        EasyPoiUtils.exportExcel(result, "门户表列表", "门户表报表", TBGateway.class, "门户表明细报表.xls", response);

    }


}

